package com.lagou.zk;

import com.mchange.v2.c3p0.ComboPooledDataSource;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class ZkConfigTest {

    /**
     * 先按默认链接池去查询一下数据库
     * 然后等待30秒，手动去zk,重新刷新配置  set /mysql/config {"dbHost":"centos1:3306","db":"azkaban","username":"root","password":"12345678"}
     * 执行第二次查询
     * @param args
     * @throws Exception
     */
    public static void main(String[] args) throws Exception {
        queryTest();
        System.out.println("查询完成,等待配置修改后查询");
        Thread.sleep(30000);
        queryTest();
    }

    /**
     * 每次获取链接查询数据库测试配置变更
     * @throws SQLException
     */
    static void queryTest() throws SQLException {
        MysqlPool mysqlPool = new MysqlPool();
        ComboPooledDataSource pools = mysqlPool.getPools();
        Connection conn = pools.getConnection();
        PreparedStatement preparedStatement = conn.prepareStatement("select upload_time,uploader,file_type from project_versions ");
        ResultSet resultSet = preparedStatement.executeQuery();
        int columnCount = resultSet.getMetaData().getColumnCount();
        while (resultSet.next()) {
            for (int i = 1; i <= columnCount; i++) {
                String value = resultSet.getString(i);
                String columnName = resultSet.getMetaData().getColumnName(i);
                System.out.print(columnName + " " + value);
            }
            System.out.println();
        }
    }
}
